/**
 * Created by admin on 2017/10/30.
 */
/**
 * Created by admin on 2017/10/26.
 */
$(document).ready(function () {
    $("form").submit(function () {
        return false;
    });
    var VAL = {
        NORMAL: "0000000",
        EVT_USER: $("#userId").text(),
        T_FBPBISOPE: 'FBPBISOPE',
        T_FBPBISTOL: 'FBPBISTOL',
        T_FBPMDLDEF: "FBPMDLDEF",
        T_FPPMOVEIN: 'FPPMOVEIN',
        T_FPPMVINCCL: 'FPPMVINCCL',
        T_FPPPACKCLM: "FPPPACKCLM",
        T_FIPINQSHT: "FIPINQSHT",
        DISABLED_ATTR: {
            'disabled': true
        },
        ENABLED_ATTR: {
            'disabled': false
        }
    };
    var domObj = {

        query_btn: $("#query_btn"),
        pack_btn: $("#pack_btn"),
        cancel_btn: $("#cancel_btn"),
        cancel_whole_btn: $("#cancel_whole_btn"),
        clean_btn :$("#clean_btn"),

        lotIdFk: $("#lotIdFk"),
        prdSeqId: $("#prdSeqId"),
      /*  factorySite: $("#factorySite"),
        factoryId: $("#factoryId"),*/
        woCate: $("#woCate"),
        woIdFk: $("#woIdFk"),
        mdlId: $("#mdlId"),
        prdGrade: $("#prdGrade"),
        lotId: $("#lotId"),

        grids: {
            $lotListDiv: $("#lotListDiv"),
            $lotListGrd: $("#lotListGrd"),
            $lotListPg: $("#lotListPg"),
        },
    }

    function initInner() {
        var lotListCM = [
            {name: 'prd_seq_id', index: 'prd_seq_id', label: '屏体代码', width: 120},
            {name: 'prd_grade', index: 'prd_grade', label: '产品等级', width: 120},
            {name: 'wo_id', index: 'wo_id', label: '工单代码', width: 120},
            {name: 'mdl_id_fk', index: 'mdl_id_fk', label: '产品代码', width: 120},
            {name: 'lot_id', index: 'lot_id', label: '批次代码', width: 120},
            {name: 'lot_cnt', index: 'lot_cnt', label: '批次需求数量', width: 130},
            {name: 'ope_id_fk', index: 'ope_id_fk', label: '站点代码', hidden:true,width: 130}
        ];
        domObj.grids.$lotListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            height: 510,
            width: domObj.grids.$lotListDiv.width(),
            autowidth: false,
            shrinkToFit: false,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            rowNum: 300,
            emptyrecords: true,
            multiselect: true,
            pager: '#lotListPg',
            colModel: lotListCM
        });
    }

    var toolFunc = {
        query_func: function () {
            var rowNum = 0;
            var prdSeqId = domObj.prdSeqId.val().trim();
            if (!prdSeqId) {
                showErrorDialog("", "请输入玻璃ID");
                return false;
            }
            var inObj = {
                trx_id: VAL.T_FIPINQSHT,
                evt_usr: VAL.EVT_USER,
                action_flg: 'Q',
                prd_seq_id: prdSeqId
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                domObj.prdSeqId.val("");
                if(outObj.prd_stat!="WAIT"){
                    showErrorDialog("",prdSeqId+"不是WAIT状态");
                    return false;
                }

                var rowIDs = domObj.grids.$lotListGrd.jqGrid("getDataIDs");
                if (rowIDs.length == 0) {
                    rowNum = 1;
                } else {
                    rowNum = parseInt(rowIDs[rowIDs.length - 1], 10) + 1;
                }

                if( rowIDs ){
                    for(var i = 0, j = rowIDs.length; i < j; i++) {
                        var curRowData = domObj.grids.$lotListGrd.jqGrid('getRowData', rowIDs[i]);
                        if(curRowData['prd_seq_id'] == prdSeqId){
                            showErrorDialog("",prdSeqId+"已经存在！");
                            return false;
                        }
                        if( curRowData['wo_id'] != outObj.wo_id_fk ){
                            showErrorDialog("","工单代码不一致!");
                            return false;
                        }
                        if( curRowData['ope_id_fk'] != outObj.ope_id_fk ){
                            showErrorDialog("","站点代码不一致!");
                            return false;
                        }

                    }
                }
               /* domObj.factorySite.val(outObj.fab_site_id_fk);
                domObj.factoryId.val(outObj.fab_id_fk);*/
                domObj.woIdFk.val(outObj.wo_id_fk);
                domObj.woCate.val(outObj.wo_cate);
                domObj.mdlId.val(outObj.mdl_id_fk);
                domObj.prdGrade.val(outObj.prd_grade);
                domObj.lotId.val(outObj.lot_id);
                var dataRow = {
                    prd_seq_id: prdSeqId,
                    prd_grade: outObj.prd_grade,
                    wo_id: outObj.wo_id_fk,
                    mdl_id_fk: outObj.mdl_id_fk,
                    lot_id: outObj.lot_id,
                    lot_cnt: outObj.batch_cnt,
                    ope_id_fk:outObj.ope_id_fk
                }
                domObj.grids.$lotListGrd.jqGrid("addRowData", parseInt(rowNum), dataRow);

            }
        },
        queryLot_func: function (lot_id) {
            var lotId;
            if(lot_id !== "" && lot_id !== undefined && lot_id !== null){
                lotId = lot_id;
            }else {
                lotId = domObj.lotIdFk.val().trim();
            }
            if (!lotId) {
                showErrorDialog("", "请输入LOT代码");
                return false;
            }
            var inObj = {
                trx_id: VAL.T_FIPINQSHT,
                evt_usr: VAL.EVT_USER,
                action_flg: 'L',
                lot_id: lotId
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary3, "#lotListGrd");
            }
        },
        pack_func: function () {
            var iary = new Array();
            var rowIDs = domObj.grids.$lotListGrd.jqGrid('getGridParam','selarrrow');
            if( rowIDs.length == 0){
                showErrorDialog("","请选择组lot的玻璃!");
                return false;
            }
            if( rowIDs ){
                for(var i = 0, j = rowIDs.length; i < j; i++) {
                    var curRowData = domObj.grids.$lotListGrd.jqGrid('getRowData', rowIDs[i]);
                    if(curRowData['lot_id']){
                        showErrorDialog("",curRowData['prd_seq_id']+"已经组lot！");
                        return false;
                    }
                    var prdInf = {
                        prd_seq_id     : curRowData['prd_seq_id']
                    }
                    iary.push( prdInf );
                }
            }
            var lotId = domObj.lotIdFk.val().trim();
            // if (!lotId) {
            //     showErrorDialog("", "请输入LOT代码");
            //     return false;
            // }
            var inObj = {
                trx_id: VAL.T_FPPPACKCLM,
                evt_usr: VAL.EVT_USER,
                action_flg: 'L',
                lot_id: lotId,
                iary : iary
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("组批次成功");
                var lot_id = outObj.lot_id;
                toolFunc.queryLot_func(lot_id);
            }
        },
        cancel_func: function () {
            var iary = new Array();
            var rowIDs = domObj.grids.$lotListGrd.jqGrid('getGridParam','selarrrow');
            if( rowIDs.length == 0){
                showErrorDialog("","请选择取消组lot的玻璃!");
                return false;
            }
            if( rowIDs ){
                for(var i = 0, j = rowIDs.length; i < j; i++) {
                    var curRowData = domObj.grids.$lotListGrd.jqGrid('getRowData', rowIDs[i]);
                    if(!curRowData['lot_id']){
                        showErrorDialog("",curRowData['prd_seq_id']+"还未组lot！");
                        return false;
                    }
                    var prdInf = {
                        prd_seq_id     : curRowData['prd_seq_id']
                    }
                    iary.push( prdInf );
                }
            }
            var inObj = {
                trx_id: VAL.T_FPPPACKCLM,
                evt_usr: VAL.EVT_USER,
                action_flg: 'O',
                iary : iary
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("取消组批次成功");

                // toolFunc.queryLot_func();
            }
        },
        cancel_whole_func: function () {
            var lotId = domObj.lotIdFk.val().trim();
            if (!lotId) {
                showErrorDialog("", "请输入LOT代码");
                return false;
            }
            var inObj = {
                trx_id: VAL.T_FPPPACKCLM,
                evt_usr: VAL.EVT_USER,
                action_flg: 'W',
                lot_id: lotId
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("取消组批次成功");
            }
        },
        clean_func: function () {
            $("input").val("");
            domObj.grids.$lotListGrd.jqGrid("clearGridData");
        }
    }
    var initializationFunc = function () {
        initInner();
    };
    initializationFunc();
    domObj.prdSeqId.keydown(function(event){
        'use strict';
        if(event.keyCode === 13){
            toolFunc.query_func();
        }
        return true;
    });
    domObj.lotIdFk.keydown(function(event){
        'use strict';
        if(event.keyCode === 13){
            toolFunc.queryLot_func();
        }
        return true;
    });
    domObj.query_btn.click(function () {
        toolFunc.queryLot_func();
    });
    domObj.pack_btn.click(function () {
        toolFunc.pack_func();
    });
    domObj.cancel_btn.click(function () {
        toolFunc.cancel_func();
    });
    domObj.cancel_whole_btn.click(function () {
        toolFunc.cancel_whole_func();
    });
    domObj.clean_btn.click(function () {
        toolFunc.clean_func();
    });
    //表格自适应
    function resizeFnc() {
        var offsetBottom, grdDivWidth;
        grdDivWidth = $("#lotListDiv").width();
        offsetBottom = $(window).height() - $("#lotListDiv").offset().top;
        $("#lotListDiv").height(offsetBottom * 0.99);
        $("#lotListGrd").setGridWidth(grdDivWidth * 0.97);
        $("#lotListGrd").setGridHeight(offsetBottom * 0.95 - 51);
    };

    resizeFnc();
    $(window).resize(function () {
        resizeFnc();
    });
});
